<template>
  <div class="mind-map">
    <!-- 一级节点容器 -->
    <div class="first-level">
      <template v-for="(item, index) in data" :key="item.id">
        <div class="branch">
          <!-- 一级节点 -->
          <div 
            class="node first-node" 
            :class="{ mastered: item.mastered }"
            @click="handleNodeClick(item)"
          >
            {{ item.title }}
          </div>
          <!-- 连接线 -->
          <div class="line-down"></div>
          <!-- 二级节点容器 -->
          <div class="second-level">
            <template v-for="child in item.children" :key="child.id">
              <div 
                class="node second-node" 
                :class="{ mastered: child.mastered }"
                @click="handleNodeClick(child, item)"
              >
                {{ child.title }}
              </div>
            </template>
          </div>
        </div>
        <!-- 分隔线 -->
        <div v-if="index !== data.length - 1" class="branch-separator"></div>
      </template>
    </div>
  </div>
</template>

<script setup>
import { defineProps, defineEmits } from 'vue'

const props = defineProps({
  data: {
    type: Array,
    required: true
  }
})

const emit = defineEmits(['update'])

const handleNodeClick = (node, parent = null) => {
  emit('update', {
    parentId: parent ? parent.id : node.id,
    id: parent ? node.id : null,
    mastered: !node.mastered
  })
}
</script>

<style scoped>
.mind-map {
  padding: 40px 20px;
  display: flex;
  justify-content: center;
  min-height: 400px;
  overflow-x: auto;
}

.first-level {
  display: flex;
  justify-content: center;
  gap: 60px;
  min-width: min-content;
}

.branch {
  display: flex;
  flex-direction: column;
  align-items: center;
  background: #f5f7fa;
  padding: 20px;
  border-radius: 8px;
  min-width: 200px;
}

.node {
  padding: 8px 16px;
  border-radius: 4px;
  background-color: #909399;
  color: white;
  cursor: pointer;
  transition: all 0.3s;
  min-width: 120px;
  text-align: center;
  font-size: 14px;
  white-space: nowrap;
}

.first-node {
  font-weight: 500;
  font-size: 16px;
  margin-bottom: 15px;
}

.node.mastered {
  background-color: #67C23A;
}

.node:hover {
  transform: scale(1.05);
  box-shadow: 0 2px 12px 0 rgba(0,0,0,0.1);
}

.second-level {
  display: flex;
  flex-direction: column;
  gap: 12px;
  width: 100%;
}

.line-down {
  width: 2px;
  height: 20px;
  background-color: #DCDFE6;
  margin-bottom: 15px;
}

.branch-separator {
  width: 2px;
  background-color: #DCDFE6;
  align-self: stretch;
}

/* 添加水平滚动条样式 */
::-webkit-scrollbar {
  height: 8px;
}

::-webkit-scrollbar-track {
  background: #f1f1f1;
  border-radius: 4px;
}

::-webkit-scrollbar-thumb {
  background: #888;
  border-radius: 4px;
}

::-webkit-scrollbar-thumb:hover {
  background: #555;
}
</style>
